export const validWebsite = (rule, value, callback) => {
  if (value === '' || value === undefined) {
    callback()
  } else {
    const reg = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/
    if (!reg.test(value)) { callback(new Error('请输入正确的网址')) } else {
      callback()
    }
  }
}
export const validRequiredWebsite = (rule, value, callback) => {
  if (value === '' || value === undefined) {
    callback(new Error('请输入网址'))
  } else {
    const reg = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/
    if (!reg.test(value)) { callback(new Error('请输入正确的网址')) } else {
      callback()
    }
  }
}

export const validMobile = (rule, value, callback) => {
  if (value === '' || value === undefined) {
    callback(new Error('请输入手机号码'))
  } else {
    const reg = /[0-9]{11}/
    if (!reg.test(value)) { callback(new Error('请输入正确的手机号码')) } else {
      callback()
    }
  }
}

export const validID = (rule, value, callback) => {
  if (value === '' || value === undefined) {
    callback(new Error('请输入身份证号码'))
  } else {
    const reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
    if (!reg.test(value)) { callback(new Error('身份证号码不正确')) } else {
      callback()
    }
  }
}

export const phoneValid = value => {
  const reg = /[0-9]{11}/
  if (!reg.test(value)) {
    return false
  } else {
    return true
  }
}

//校验身份证号码
export function validateIDCard(value) {
  var format = /^(([1][1-5])|([2][1-3])|([3][1-7])|([4][1-6])|([5][0-4])|([6][1-5])|([7][1])|([8][1-2]))\d{4}(([1][9]\d{2})|([2]\d{3}))(([0][1-9])|([1][0-2]))(([0][1-9])|([1-2][0-9])|([3][0-1]))\d{3}[0-9xX]$/;
  //号码规则校验
  if (!format.test(value)) {
    return false;
  }
  //区位码校验
  //出生年月日校验   前正则限制起始年份为1900;
  var year = value.substr(6, 4), //身份证年
    month = value.substr(10, 2), //身份证月
    date = value.substr(12, 2), //身份证日
    time = Date.parse(month + "-" + date + "-" + year), //身份证日期时间戳date
    now_time = Date.parse(new Date()), //当前时间戳
    dates = new Date(year, month, 0).getDate(); //身份证当月天数
  if (time > now_time || date > dates) {
    return false;
  }
  //校验码判断
  var c = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; //系数
  var b = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"]; //校验码对照表
  var id_array = value.split("");
  var sum = 0;
  for (var k = 0; k < 17; k++) {
    sum += parseInt(id_array[k]) * parseInt(c[k]);
  }
  return id_array[17].toUpperCase() === b[sum % 11].toUpperCase();
}